import torch.nn as nn
import torchvision
import torch

model = torchvision.models.vgg16()

data = []
outputs = model(data)
target = []
loss_fun = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(model.parameters(),lr=0.001)
# 计算loss
loss = loss_fun(outputs,target)

# 梯度清零
optimizer.zero_grad()

# 反向传递
loss.backward()

#  梯度裁剪
# max_norm – 梯度的最大范数
# max_norm – 梯度的最大范数
# norm_type – 规定范数的类型，默认为L2
torch.nn.utils.clip_grad_norm(model.parameters(),max_norm=20,norm_type=2)

# 更新参数
optimizer.step()